nice_things/macros/strict_mode.macro.sh
lenient_mode
Since 0.3.0 · Source
Synopsis#{{{ lenient_mode }}}
Configuration
–
Description
Revert the configuration changes made by the strict_mode macro back to defaults.
Options
–
Operands
–
Stdin
–
Stdout
Two lines of code are printed.
Stderr
–
Exit status
0: Successful completion.123: Unexpected error (abort).
Abort
Aborts on unexpected error.
Usage examples
#{{{ lenient_mode }}}
strict_mode
Since 0.3.0 · Source
Synopsis#{{{ strict_mode }}}
Configuration
[package:nice_things]
# Value to be assigned to the IFS variable in strict_mode; accepts printf-style escape sequences
strict_mode_ifs=\037
Description
Change shell options to make behavior more predictable and robust. The line of code printed by this macro does two things, it sets shopts set -Cefu, and it changes the value of IFS to \037 (Unit Separator control code 0x1F). The default value of IFS can be overridden in nice_package.conf.
set -C: (noclobber) Prevent existing files from being overwritten by the shell's>redirection operator. Use the explicit overwrite operator>|when overwriting is desired.set -e: (errexit) Exit immediately if any untested command fails.set -f: (noglob) Disable pathname expansion. Useglobto perform globbing.set -u: (nounset) Exit immediately when attempting to expand a variable that is not set.
This configuration can be reverted to defaults using the accompanying lenient_mode macro.
Note
You may have noticed the omission of a
pipefailoption instrict_mode. That is becausepipefailis a bash feature and is not available in other POSIX shells. Still, checking the return status of every stage of a pipeline for errors is very important for program robustness. For that reason the framework offers an equivalent alternative in thePipeStatusclass.
Options
–
Operands
–
Stdin
–
Stdout
One line of code is printed.
Stderr
–
Exit status
0: Successful completion.123: Unexpected error (abort).
Abort
Aborts on unexpected error.
Usage examples
#!/bin/sh
# shellcheck disable=SC2046,SC2086
#{{{ strict_mode }}}